package work4;

public class FizzBuzz {
	private int num;
	private static int count=1;
	private  boolean[] flag=new boolean[num+1];
	private  final Object lock=new Object();
	public int getCount() {
		return count;
	}

	/*public void setCount(int count) {
		this.count = count;
	}
*/
	public boolean[] getFlag() {
		return flag;
	}

	public void setFlag(boolean[] flag) {
		this.flag = flag;
	}

	public Object getLock() {
		return lock;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public FizzBuzz(int num) {
		this.num = num;
	}
	public FizzBuzz(){
		
	}
	
	/*public FizzBuzz(int num, int count) {
		this.num = num;
		this.count = count;
	}*/

	public void fizz() {
		while(true){
			synchronized (lock) {
				if(count > 20){
					lock.notifyAll();
					break;
				}else{
					//if (!flag[count]){
						if (count%3==0 && count%5!=0){
							System.out.println("fizz");
							//flag[count] = true;
							count++;
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}else if (count%3!=0 && count%5!=0){
							lock.notifyAll();
						}
					//}
				}
			}
		}
	}  
	
	public void buzz() {
		while(true){
			synchronized (lock){
				if(count > 20){
					lock.notifyAll();
					break;
				}else{
					//if (!flag[count]){
						if (count%3!=0 && count%5==0){
							System.out.println("buzz");
							//flag[count] = true;
							count++;
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}else if (count%3!=0 && count%5!=0){
							lock.notifyAll();
						}
					//}
				}
			}
		}
	}         
	  
	public void fizzbuzz() {
		while(true){
			synchronized (lock){
				if(count > 20){
					lock.notifyAll();
					break;
				}else{
					//if (!flag[count]){
						if (count%3==0 && count%5==0){
							System.out.println("fizzbuzz");
							//flag[count] = true;
							count++;
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}else if (count%3!=0 && count%5!=0){
							lock.notifyAll();
						}
					//}
				}
			}
		}
	
	} 
	
	public void number() {
		while(true){
			synchronized (lock){
				if(count > 20){
					lock.notifyAll();
					break;
				}else{
					//if (!flag[count]){
						if (count%3!=0 && count%5!=0){
							System.out.println(count);
							//flag[count] = true;
							count++;
							try {
								lock.wait();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}else {
							lock.notifyAll();
						}
					//}
				}
			}
		}
	} 
}
